package com.universal.schedule;

import android.app.Activity;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.universal.schedule.db.DBManager;
import com.universal.schedule.db.DatabaseHelper;

public class AddScheduleActivity extends Activity {

	private EditText mEditText1;
	private EditText mEditText2;
	private TextView mTv5;
	private TextView mTv7;
	private EditText mTv9;
	private TextView mPublic;
	private int week;
	private int section;
	private int sectionPart;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_add_schedule);
		findView();
		setListener();
	}


	private void setListener() {
		mTv5.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				shooseWeek();

			}

		});

		mTv7.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				shooseSection();
			}
		});

		mPublic.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if(!(mEditText1.getText().toString().equals("")&&
						mEditText2.getText().toString().equals("")&&
						mTv5.getText().toString().equals("")&&
						mTv7.getText().toString().equals("")&&
						mTv9.getText().toString().equals(""))){



					ContentValues cv = new ContentValues();
					cv.put(DatabaseHelper.COLUMN_SCHEDULE_COURSEName, mEditText1.getText().toString());
					cv.put(DatabaseHelper.COLUMN_SCHEDULE_TEACHERNAME, mEditText2.getText().toString());
					cv.put(DatabaseHelper.COLUMN_SCHEDULE_SECTION, String.valueOf(section));
					cv.put(DatabaseHelper.COLUMN_SCHEDULE_SECTIONSPAN, String.valueOf(sectionPart));
					cv.put(DatabaseHelper.COLUMN_SCHEDULE_WEEK, String.valueOf(week));
					cv.put(DatabaseHelper.COLUMN_SCHEDULE_CLASSROOM, mTv9.getText().toString());
					cv.put(DatabaseHelper.COLUMN_SCHEDULE_COURSEFLAG, String.valueOf((int) (Math.random() * 10)));
					long insert = DBManager.getInstance(AddScheduleActivity.this).insert(DatabaseHelper.SCHEDULE_TABLE,
							DatabaseHelper.COLUMN_SCHEDULE_COURSEName, cv);
					Toast.makeText(AddScheduleActivity.this, "添加成功", Toast.LENGTH_LONG).show();
					setResult(0);
					finish();
				}else{
					Toast.makeText(AddScheduleActivity.this, "有些还未完善,请完善好,再添加", Toast.LENGTH_LONG).show();
				}
			}
		});
	}




	private void findView() {
		mEditText1 = (EditText) findViewById(R.id.editText1);
		mEditText2 = (EditText) findViewById(R.id.editText2);
		mTv5 = (TextView) findViewById(R.id.textView5);
		mTv7 = (TextView) findViewById(R.id.textView7);
		mTv9 = (EditText) findViewById(R.id.textView9);
		mPublic = (TextView) findViewById(R.id.edit_public);

	}


	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		switch (item.getItemId()) {
			case android.R.id.home:
				finish();
				break;

			default:
				break;
		}
		return super.onOptionsItemSelected(item);
	}

	private void shooseWeek() {
		new AlertDialog.Builder(AddScheduleActivity.this)
				.setTitle("请选择上课时间")
				.setItems(new String[] {"星期一","星期二","星期三","星期四","星期五","星期六","星期日"}, new android.support.v7.app.AlertDialog.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						week=which+1;
						switch (which) {
							case 0:
								mTv5.setText("星期一");
								break;
							case 1:
								mTv5.setText("星期二");
								break;
							case 2:
								mTv5.setText("星期三");
								break;
							case 3:
								mTv5.setText("星期四");
								break;
							case 4:
								mTv5.setText("星期五");
								break;
							case 5:
								mTv5.setText("星期六");
								break;
							case 6:
								mTv5.setText("星期日");
								break;
							default:
								break;
						}
						dialog.dismiss();

					}
				})
				.show();
	}

	protected void shooseSection() {
		new AlertDialog.Builder(AddScheduleActivity.this)
				.setTitle("选择第几节")
				.setItems(new String[] {"1","2","3","4","5","6","7","8","9","10","11","12"}, new android.support.v7.app.AlertDialog.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						section=which+1;
						switch (which) {
							case 0:
								choosepart();
								break;
							case 1:
								choosepart();
								break;
							case 2:
								choosepart();
								break;
							case 3:
								choosepart();
								break;
							case 4:
								choosepart();
								break;
							case 5:
								choosepart();
								break;
							case 6:
								choosepart();
								break;
							case 7:
								choosepart();
								break;
							case 8:
								choosepart();
								break;
							case 9:
								choosepart();
								break;
							case 10:
								choosepart();
								break;
							case 11:
								choosepart();
								break;
							case 12:
								choosepart();
								break;
							default:
								break;
						}
						dialog.dismiss();
					}
				})
				.show();

	}


	protected void choosepart() {
		new AlertDialog.Builder(AddScheduleActivity.this)
				.setTitle("选择连续上几节课")
				.setItems(new String[] {"1","2","3",}, new android.support.v7.app.AlertDialog.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						sectionPart=which+1;
						switch (which) {
							case 0:
								mTv7.setText(section+"-"+(section+sectionPart-1)+"节");
								break;
							case 1:
								mTv7.setText(section+"-"+(section+sectionPart-1)+"节");
								break;
							case 2:
								mTv7.setText(section+"-"+(section+sectionPart-1)+"节");
								break;
							default:
								break;
						}
					}
				})
				.show();
	}
}
